/*
 * File:   main.cpp
 * Author: jay sprenkle
 *
 * Created on October 11, 2012
 */
#include <QtGui/QApplication>
#include <QApplication>
#include <QMainWindow>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QLabel>

#include "UI.RoundGraph.h"

// <editor-fold defaultstate="collapsed" desc="View class">
class Controller;

class View
: public QMainWindow
{
//   Q_OBJECT     // DO NOT INCLUDE if there are no signals or slots
public:
   View( Controller* ctrl );

   void paintEvent( QPaintEvent* event );

   Controller*                _Controller;
   UI::RoundGraph*            graph;
   QPropertyAnimation*        GraphValueAnimation;
   QPropertyAnimation*        GraphAlphaAnimation;
   QParallelAnimationGroup*   GraphAnimationGroup;
   QLabel*                    Text;
   QLabel*                    BackgroundImage;
};

View::View( Controller* ctrl )
   : _Controller( ctrl )
   , graph( 0 )
   , Text( 0 )
   , BackgroundImage( 0 )
{
   resize( 800, 546 );
   QSizePolicy szp( QSizePolicy::Fixed, QSizePolicy::Fixed );
   szp.setHorizontalStretch( 0 );
   szp.setVerticalStretch( 0 );
   szp.setHeightForWidth( sizePolicy().hasHeightForWidth() );
   setSizePolicy( szp );

   BackgroundImage = new QLabel( this );
   BackgroundImage->setPixmap( QPixmap( ":wallpaper-1074980.jpg" ) );
   BackgroundImage->setGeometry( 0, 0, 800, 546 );

   // setup a graph
   graph = new UI::RoundGraph( dynamic_cast<QWidget*>( this ) );
   // setGeometry( int x, int y, int w, int h )
   graph->setGeometry( 8, 8, 64, 64 );
   graph->setColor( QColor( Qt::white ) );
   graph->setBackgroundColor( QColor( Qt::darkGray ) );
   // 5 o'clock = 0, 2 o'clock = 100%
   graph->setStartAngle( 170 );
   graph->setSpanAngle( 280 );
   graph->setGraphWidth( 7 );

//   graph->setValue( 1.0f );

   Text = new QLabel( this );
   Text->setStyleSheet( QString( "QWidget { color: white; }" ) );
   Text->setGeometry( 64, 58, 96, 12 );

   // do a guage sweep
   GraphValueAnimation = new QPropertyAnimation( graph, "Value" );
   GraphValueAnimation->setDuration( 1000 );
   GraphValueAnimation->setKeyValueAt( 0.0f, QVariant( 0.0f ) );
   GraphValueAnimation->setKeyValueAt( 1.0f, QVariant( 1.0f ) );

   GraphAlphaAnimation = new QPropertyAnimation( graph, "Alpha" );
   GraphAlphaAnimation->setDuration( 1000 );
   GraphAlphaAnimation->setKeyValueAt( 0.0f, QVariant( 0.0f ) );
   GraphAlphaAnimation->setKeyValueAt( 1.0f, QVariant( 0.9f ) );

   GraphAnimationGroup = new QParallelAnimationGroup;
   GraphAnimationGroup->addAnimation( GraphValueAnimation );
   GraphAnimationGroup->addAnimation( GraphAlphaAnimation );
   GraphAnimationGroup->start();
}

void View::paintEvent( QPaintEvent* event )
{
   if ( graph && Text )
      Text->setText( QString( "Temp: %1C" ).arg( (int)( graph->getValue() * 50 ) ) );

   QWidget::paintEvent( event );
}

// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Controller class">

class Controller
: public QApplication
{
//   Q_OBJECT     // DO NOT INCLUDE if there are no signals or slots
public:
   Controller( int argc, char *argv[] );

   void Exit();

private:
   View* mView;
};

Controller::Controller( int argc, char *argv[] )
: QApplication( argc, argv )
{
   // create a default view
   mView = new View( this );
   mView->show();
}

void Controller::Exit()
{
   exit( 0 );
}
// </editor-fold>

int main( int argc, char** argv )
{
   // initialize Qt resources
   Q_INIT_RESOURCE( images );

   Controller app( argc, argv );
   return app.exec();
   return EXIT_SUCCESS;
}

